package com.qf.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsUserMapper;
import com.qf.pojo.DtsUser;
import com.qf.vo.DayStatis;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author mkr
 * @version V1.0
 * @Project java2203-mkr
 * @Package com.qf.service
 * @Description:
 * @Date 2022/6/17 9:22
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private DtsUserMapper userMapper;


    @Override
    public int countUser() {
        QueryWrapper<DtsUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", false);
        Integer count = userMapper.selectCount(queryWrapper);
        return count;
    }

    @Override
    public List<DayStatis> incrCount() {
        List<DayStatis> list = userMapper.staticIncreaseUser();
        return list;
    }

    @Override
    public IPage<DtsUser> VipList(String username, String mobile, Integer page,
                                 Integer limit, String sort, String order) {

        //创建分页对象
        IPage<DtsUser> iPage = new Page<>(page,limit);

        //封装查询对象
        QueryWrapper<DtsUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", false);

        //判断查询条件是否为空
        if (!StringUtils.isEmpty(username)){
            queryWrapper.eq("username", username);
        }
        if (!StringUtils.isEmpty(mobile)){
            queryWrapper.eq("mobile", mobile);
        }
        //判断排序字段是否为空
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)){
            //判断是否为升序排序
            if (order.equalsIgnoreCase("asc")){
                queryWrapper.orderByAsc(sort);
            }else {
                queryWrapper.orderByDesc(sort);
            }
        }
        //查询并返回
        IPage<DtsUser> VipIPage = userMapper.selectVipList(iPage, queryWrapper);
        return VipIPage;
    }

    @Override
    public void approveAgency(DtsUser user , Integer settlement_rate) {

        //封装对象
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_level", 2);
        //判断条件参数是否为空
        if (settlement_rate != null){
            queryWrapper.eq("settlement_rate", settlement_rate);
        }

        userMapper.update(user,queryWrapper);
     }
}
